/**
 * main function of cdragon
 * Copyright (C) 2009 Hajime Yoshimori <RoarLudros@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include <DragonExcept>
#include <DragonInit>
#include <DragonPrintf>

#include "cdragon.h"

#include <iostream>
#include <locale>
#include <libintl.h>

using namespace std;

int main(int argc, char** argv) {
  try {
	setlocale(LC_ALL, "");
	wcout.imbue(locale(""));

	bindtextdomain(CDRAGON_DOMAIN_NAME, MO_INSTALL_PATH);
	textdomain(CDRAGON_DOMAIN_NAME);
	
	cdragon::cdragonapp theApp(argc, argv);
	
	return theApp.exec();
	
  } catch(dragon::DragonExcept& ex) {
	if(ex.getFileName() != "") {
	  if(ex.getLineNum() != 0) {
		/* TRANSLATORS: First argument is the name of dragon (CDragon).
		   second argument is the file name.
		   third argument is line number.
		   forth argument is the message. */
		cout << dragon::sprintf(dragon::_("%1$s: %2$s(%3$d): %4$s").c_str(), 
		/* TRANSLATORS: Dragon's name. "C" (onsole) + "Dragon".
		   If it isn't suitable to your language, you can use
		   only "dragon". */
								dragon::_("CDragon").c_str(), 
								ex.getFileName().c_str(),
								ex.getLineNum(),
								ex.getDescription().c_str()) << endl;
	  } else {
		/* TRANSLATORS: First argument is the name of dragon (CDragon).
		   second argument is the file name.
		   third argument is the message. */
		cout << dragon::sprintf(dragon::_("%1$s: %2$s: %3$s").c_str(), 
								dragon::_("CDragon").c_str(), 
								ex.getFileName().c_str(),
								ex.getDescription().c_str()) << endl;
	  }
	} else {
	  /* TRANSLATORS: First argument is the name of dragon (CDragon).
	     second argument is the message. */
	  cout << dragon::sprintf(dragon::_("%1$s: %2$s").c_str(),
							  dragon::_("CDragon").c_str(), 
							  ex.getDescription().c_str()) << endl;
	}
	return 037;
  }
}
